package com.victor.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.victor.entity.User;
import com.victor.entity.toreport.FrozenContent;
import com.victor.entity.toreport.ToReport;
import com.victor.service.ToreportService;
import com.victor.service.UserService;
import com.victor.utils.ClassFilter;
import com.victor.utils.Msg;

/**
 * 冻结 举报
 * 
 * @author
 *
 */

@RestController
@RequestMapping("/frozentoreport")
public class ToreportComtroller {
	@Autowired
	private ToreportService service;
	@Autowired
	private UserService userService;
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");// 设置日期格式

	/**
	 * 用户列表冻结
	 * 
	 * @param user
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/frozenuser")
	public Msg frozen(User user, String title, String content,String frozen_cycle) throws ParseException {
		String[] str = { "id", "frozen_starttime", "frozen_endtime" };
		boolean empty = ClassFilter.somePropertyIsEmpty(user, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		//添加冻结内容
		FrozenContent frozenContent = new FrozenContent();
		frozenContent.setFrozenreason(title + content);
		frozenContent.setFrozentime(frozen_cycle);
		frozenContent.setUid(String.valueOf(user.getId()));
		boolean c = service.insertFrozenContent(frozenContent);
		if (!c) {
			return Msg.fail("添加原因失败");
		}
		//修改用户为冻结状态 开始结束时间
		user.setFrozen("1");
		boolean b = userService.updateUser(user);
		return Msg.judge(b);
	}

	/**
	 * 批量冻结 的冻结
	 * 
	 * @param users
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/muchfrozen")
	public Msg muchFrozen(String[] uids, String frozen_cycle, String title, String content, String starttime,
			String endtime) throws ParseException {
		if (uids == null || uids.length <= 0 || frozen_cycle == null || frozen_cycle.isEmpty() || title == null
				|| title.isEmpty() || content == null || content.isEmpty()) {
			return Msg.fail("入参错误");
		}
		for (int j = 0; j < uids.length; j++) {
			//添加冻结内容
			FrozenContent frozenContent = new FrozenContent();
			frozenContent.setFrozenreason(title + content);
			frozenContent.setFrozentime(frozen_cycle);
			frozenContent.setUid(uids[j]);
			boolean c = service.insertFrozenContent(frozenContent);
			if (!c) {
				return Msg.fail("添加原因失败");
			}
			//修改用户冻结状态 开始结束时间
			User user = new User();
			user.setId(Integer.valueOf(uids[j]));
			user.setFrozen("1");
			user.setFrozen_starttime(starttime);
			user.setFrozen_endtime(endtime);
			boolean b = userService.updateUser(user);
			if (!b) {
				return Msg.fail("修改用户失败");
			}
		}

		return Msg.success();
	}


	/**
	 * 违规信息 列表
	 * 
	 * @param uid
	 * @param page
	 * @param row
	 * @return
	 */
	@RequestMapping("/wronginfo")
	public Msg wrongInfo(String uid, int page, int row) {
		if (uid == null || uid.isEmpty()) {
			return Msg.fail("入参错误");
		}
		//举报次数
		String fromcount = service.selByFromuid(uid);
		//被举报次数
		String tocount = service.selByTouid(uid);
		//举报取消数
		String delcount = service.selByFromuidAndDelstate(uid);
		//举报取消率
		double del;
		if(fromcount == "0" || fromcount.equals("0")) {
			del=0.0;
		}else {
			del = Double.valueOf(delcount) / Double.valueOf(fromcount);
		}
		PageInfo<ToReport> pageInfo = service.selwrongInfo(uid, page, row);
		return Msg.success().add("fromcount", fromcount).add("tocount", tocount).add("delcount", delcount)
				.add("del", del).add("pageInfo", pageInfo);
	}

	/**
	 * 举报详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/details")
	public Msg findXiangqing(String id) {
		if (id == null || id.isEmpty()) {
			return Msg.fail("入参错误");
		}
		ToReport selById = service.selById(id);
		return Msg.success().add("info", selById);
	}

	/**
	 * 违规详情 冻结
	 * 
	 * @param user
	 * @param toReport
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/frozendetails")
	public Msg wrongfrozen(User user, ToReport toReport,String title,String content,String frozen_cycle) throws ParseException {
		//修改用户冻结状态  开始结束时间
		String[] str = { "id", "frozen_starttime", "frozen_endtime" };
		boolean empty = ClassFilter.somePropertyIsEmpty(user, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		user.setFrozen("1");
		boolean b = userService.updateUser(user);
		if (!b) {
			return Msg.fail("修改用户失败");
		}
		//添加冻结内容表
		FrozenContent frozenContent = new FrozenContent();
		frozenContent.setFrozenreason(title+content);
		frozenContent.setFrozentime(frozen_cycle);
		frozenContent.setRoots(String.valueOf(toReport.getId()));
		frozenContent.setUid(String.valueOf(user.getId()));
		boolean c = service.insertFrozenContent(frozenContent);
		if (!c) {
			return Msg.fail("添加原因失败");
		}
		//修改举报表 处理人 处理日期
		String[] strings = { "type", "cid", "adminname" };
		boolean empty2 = ClassFilter.somePropertyIsEmpty(toReport, strings);
		if (empty2 == true) {
			return Msg.fail("评论相关为空");
		}
		toReport.setResultstart(user.getFrozen_starttime());
		toReport.setResultend(user.getFrozen_starttime());
		boolean updateByTypeAndCid = service.updateByTypeAndCid(toReport);
		return Msg.judge(updateByTypeAndCid);
	}

	/**
	 * 违规详情 追加冻结
	 * 
	 * @param user
	 * @param toReport
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/addfrozen")
	public Msg addfrozentime(User user, ToReport toReport,String title,String content,String frozen_cycle) throws ParseException {
		//修改用户的  结束时间
		String[] str = { "id", "frozen_endtime" };
		boolean empty = ClassFilter.somePropertyIsEmpty(user, str);
		if (empty == true) {
			return Msg.fail("入参错误");
		}
		boolean b = userService.updateUser(user);
		if (!b) {
			return Msg.fail("修改用户失败");
		}
		// 添加冻结原因
		FrozenContent frozenContent = new FrozenContent();
		frozenContent.setFrozenreason(title+content);
		frozenContent.setFrozentime(frozen_cycle);
		frozenContent.setRoots(String.valueOf(toReport.getId()));
		frozenContent.setUid(String.valueOf(user.getId()));
		boolean c = service.insertFrozenContent(frozenContent);
		if (!c) {
			return Msg.fail("添加原因失败");
		}
		// 修改举报的状态
		String[] strings = { "type", "cid", "adminname" };
		boolean empty2 = ClassFilter.somePropertyIsEmpty(user, strings);
		if (empty2 == true) {
			return Msg.fail("评论相关为空");
		}
		toReport.setResultend(user.getFrozen_starttime());
		boolean updateByTypeAndCid = service.updateByTypeAndCid(toReport);
		return Msg.judge(updateByTypeAndCid);

	}
}
